<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Flash extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
      // Call the Model constructor
      parent::_construct();


	}
	function create_xml($year,$month,$tourId)
	{

		$result=$this->db->query('Select * from tours where id="'.$tourId.'"');
		if ($result->num_rows() > 0)
		{
   			$row = $result->row();
			$result_to_xml['title']  =  $row->tourTitle;
			$result_to_xml['days']   =  $row->days;
			$result_to_xml['nights'] =  $row->nights;
			$result_to_xml['year'] =  $year;
			$result_to_xml['month'] =  $month;
			$result_to_xml['home'] =  $row->home;
			$result_to_xml['e1'] =  $row->ekskurs1;
			$result_to_xml['e2'] =  $row->ekskurs2;
			$result_to_xml['e3'] =  $row->ekskurs3;
			$result_to_xml['e4'] =  $row->ekskurs4;
			$result_to_xml['e5'] =  $row->ekskurs5;
			$result_to_xml['e6'] =  $row->ekskurs6;
			$result_to_xml['podarok'] =  $row->podarok;
			if($row->img=="")
			{
				$result_to_xml['photo'] =  "";
			}
			else
			{
				$result_to_xml['photo'] =   $row->img;
			}

		}
		else
		{
			$this->Log('Dont have this tourId in table '.$year.' - '.$month.' - '.$tourId);
			die();
		}
		$result_to_xml['arraydays']=array();
		if($month==12)
		{
			$nMonth=1;
			$nYear=$year+1;
		}
		else
		{
			$nMonth=$month+1;
			$nYear=$year;
		}

		$query='Select type, date,price_id from dates where date>="'.$year.'-'.$month.'-01" and date<"'.$nYear.'-'.$nMonth.'-01" and tour_id="'.$tourId.'" order by date asc';
		$result=$this->db->query($query);
		$data_prev="";
		$i=-1;
		foreach($result->result() as $row)
		{
			if($data_prev!=$row->date)
			{
				$data_prev=$row->date;
				$i=(int)substr($row->date,8);
				//$result_to_xml['arraydays'][$i]['value']=substr($row->date,8);
			}
			//$result_to_xml['arraydays'][$i]['hoteltype']['type']=$row->type;
			$query="select * from prices where id='".$row->price_id."'";
			$res=$this->db->query($query);
			if ($res->num_rows() > 0)
			{
   				$row2 = $res->row();
   			}
   			else
   			{
				$this->Log('DataBase Error: no prices');
				die();
			}
			$result_to_xml['arraydays'][$i][$row->type]['DBL']	= $row2->DBL;
			$result_to_xml['arraydays'][$i][$row->type]['SNGL']	= $row2->SNGL;
			$result_to_xml['arraydays'][$i][$row->type]['TRPL']	= $row2->TRPL;
			$result_to_xml['arraydays'][$i][$row->type]['CHLD']	= $row2->CHLD;
			$result_to_xml['arraydays'][$i][$row->type]['HB']	= $row2->HB;
		}
		return $result_to_xml;
	}
	function month($year,$month){

		if($month==12)
		{
			$nMonth=1;
			$nYear=$year+1;
		}
		else
		{
			$nMonth=$month+1;
			$nYear=$year;
		}

		$result_to_xml="";
		$query='Select COUNT(*) as count from dates where date>="'.$nYear.'-'.$nMonth.'-01"';
		$result=$this->db->query($query);
		if ($result->num_rows() > 0)
		{
   			if($result->row()->count>0)
   			{
				$result_to_xml['NextMonth'] =  'Yes';
			}
			else
			{
				$result_to_xml['NextMonth'] =  'No';
			}
   		}

   		return $result_to_xml;
	}
}